package com.study.zzedu.ui.forgetpwd;


import android.content.Context;
import android.text.TextUtils;

import com.study.zzedu.base.BaseModel;
import com.study.zzedu.http.OnResultListener;
import com.study.zzedu.utils.AppConstant;
import com.study.zzedu.utils.LogUtil;
import com.study.zzedu.utils.OkHttpUtils;

import org.json.JSONArray;
import org.json.JSONObject;

public class ForgetPwdModel extends BaseModel {
    /*
    发送手机验证码
    */
    public void reSendPhoneCode(Context context, final OnResultListener resultListener) {
        OkHttpUtils.getInstance(context).getJsonForString(OkHttpUtils.BASE_URL + "/code/reSendPhoneCode",
                new String[]{"codeKey"}, new String[]{AppConstant.codeKey}, new OkHttpUtils.HttpCallBack() {

            @Override
            public void onSuccess(String obj) {
                try {
                    JSONObject jObject = new JSONObject(obj);
                    if (0 == jObject.getInt("code")) {
                        JSONArray jsonArray = jObject.getJSONArray("data");
                        if (jsonArray.length() > 0) {
                            String uuid = jsonArray.getJSONObject(0).getString("uuid");
                            LogUtil.e("Forget", "codeUUID:" + uuid);
                            AppConstant.codeUUID = uuid;
                        }
                        resultListener.onSuccess(obj);
                    } else {
                        AppConstant.codeUUID = "";
                        resultListener.onError(jObject.get("message").toString());
                    }
                } catch (Exception e) {
                    e.toString();
                }
            }

            @Override
            public void onSuccess(JSONObject obj) {
                resultListener.onSuccess(obj.toString());
            }

            @Override
            public void onError(String msg) {
                AppConstant.codeUUID = "";
                resultListener.onError(msg);
            }
        });
    }
    public void getImageCode(Context context, final OnResultListener resultListener) {
        OkHttpUtils.getInstance(context).getJsonForString(OkHttpUtils.BASE_URL + "/code/getImageCode",
                null, null, new OkHttpUtils.HttpCallBack() {
                    @Override
                    public void onSuccess(String obj) {
                        try {
                            JSONObject jObject = new JSONObject(obj);
                            if (0 == jObject.getInt("code")) {
                                resultListener.onSuccess(obj);
                            } else {
                                resultListener.onError(jObject.get("message").toString());
                            }
                        } catch (Exception e) {
                            e.toString();
                        }
                    }

                    @Override
                    public void onSuccess(JSONObject obj) {
                        resultListener.onSuccess(obj.toString());
                    }

                    @Override
                    public void onError(String msg) {
                        resultListener.onError(msg);
                    }
                });
    }
    /*
    进行身份验证*/
    public void identify(Context context,String code, String codeKey, final OnResultListener resultListener) {
        if (TextUtils.isEmpty(AppConstant.codeUUID)) {
            LogUtil.e("Forget", "codeUUID异常");
            return;
        }
        OkHttpUtils.getInstance(context).getJsonForString(OkHttpUtils.BASE_URL + "/code/checkUserByPhoneCode",
                new String[]{"code", "codeKey"}, new String[]{code, codeKey}, new OkHttpUtils.HttpCallBack() {

                    @Override
                    public void onSuccess(String obj) {
                        try {
                            JSONObject jObject = new JSONObject(obj);
                            if (0 == jObject.getInt("code")) {
                                LogUtil.e("Forget", "身份验证成功");
                                JSONArray jsonArray = jObject.getJSONArray("data");
                                if (jsonArray.length() > 0) {
                                    AppConstant.idCodeUUID = jsonArray.getJSONObject(0).getString("uuid");
                                }
                                resultListener.onSuccess(obj);
                            } else {
                                LogUtil.e("Forget", "身份验证失败");
                                resultListener.onError(jObject.get("message").toString());
                            }
                        } catch (Exception e) {
                            e.toString();
                        }
                    }

                    @Override
                    public void onSuccess(JSONObject obj) {
                        resultListener.onSuccess(obj.toString());
                    }

                    @Override
                    public void onError(String msg) {
                        resultListener.onError(msg);
                    }
                });
    }

    /*
        修改密码*/
    public void modify(Context context, String pwd, final OnResultListener resultListener) {
        if (TextUtils.isEmpty(AppConstant.userCode)) {
            LogUtil.e("Forget", "学号异常");
            return;
        }
        OkHttpUtils.getInstance(context).getJsonForString(OkHttpUtils.BASE_URL + "/code/updatePasswordForget",
                new String[]{"pwd", "codeKey"}, new String[]{pwd, AppConstant.idCodeUUID}, new OkHttpUtils.HttpCallBack() {
                    @Override
                    public void onSuccess(String obj) {
                        try {
                            JSONObject jObject = new JSONObject(obj);
                            if (0 == jObject.getInt("code")) {
                                LogUtil.e("Forget", "密码修改成功");

                                resultListener.onSuccess(obj);
                            } else {
                                LogUtil.e("Forget", "密码修改失败");
                                resultListener.onError(jObject.get("message").toString());
                            }
                        } catch (Exception e) {
                            e.toString();
                        }
                    }

                    @Override
                    public void onSuccess(JSONObject obj) {
                        resultListener.onSuccess(obj.toString());
                    }

                    @Override
                    public void onError(String msg) {
                        LogUtil.e("Forget", "密码修改失败");
                        resultListener.onError(msg);
                    }
                });
    }
    /*根据学号获取手机号*/
    public void getPhoneByUserCode(Context context, String userCode, String code,String codeKey,final OnResultListener resultListener) {

        OkHttpUtils.getInstance(context).getJsonForString(OkHttpUtils.BASE_URL + "/code/getPhoneByUserCode",
                new String[]{"userCode", "code", "codeKey"}, new String[]{userCode,code,codeKey}, new OkHttpUtils.HttpCallBack() {
                    @Override
                    public void onSuccess(String obj) {
                        try {
                            JSONObject jObject = new JSONObject(obj);
                            if (0 == jObject.getInt("code")) {
                                LogUtil.e("Forget", "获取手机号成功");
                                resultListener.onSuccess(obj);
                            } else {
                                LogUtil.e("Forget", "获取手机号失败");
                                resultListener.onError(jObject.get("message").toString());
                            }
                        } catch (Exception e) {
                            e.toString();
                        }
                    }

                    @Override
                    public void onSuccess(JSONObject obj) {
                        resultListener.onSuccess(obj.toString());
                    }

                    @Override
                    public void onError(String msg) {
                        LogUtil.e("Forget", "获取手机号失败");
                        resultListener.onError(msg);
                    }
                });
    }

}
